<template>
  <div class="config-content" >
    <x-from v-show="dataTemp" id="configForm" ref="from" :form-data="data.arr" :form-value="data.value" :form-config="{labelPosition: 'top'}" @change="setValue">
      <div v-if="optionArr" slot="afterSlot2" class="dictionary">
        <div class="title">
          字典
        </div>
        <div v-for="(item,index) in optionArr" :key="index" class="list">
          <el-col :span="10">
            <el-input v-model="item.label" placeholder="key"/>
          </el-col>
          <el-col :span="1">
            <div class="line">-</div>
          </el-col>
          <el-col :span="10">
            <el-input v-model="item.value" placeholder="value"/>
          </el-col>
          <el-col :span="3">
            <i class="el-icon-remove-outline" type="danger" @click="delOption(index)"/>
          </el-col>
        </div>
        <div class="but-box">
          <el-button class="but" type="primary" plain round size="mini" @click="addOption">添加选项</el-button>
        </div>
      </div>
    </x-from>
    <div v-show="!dataTemp">
      还没选中任何组件
    </div>
  </div>
</template>
<script>
import xFrom from './formBox'
import configData from './setting.js'
// import formItemData from './formItem.js'
export default {
  components: {
    xFrom
  },
  props: {

  },
  data() {
    return {
      data: [],
      optionArr: null,
      dataTemp: null,
      EleIndex: null
    }
  },
  computed: {

  },
  created() {
    this.data = this.initComponentData(configData)
  },
  methods: {
    delOption(n) {
      this.optionArr.splice(n, 1)
    },
    addOption() {
      this.optionArr.push({
        key: '',
        value: ''
      })
    },
    setValue(e) {
      if (!this.dataTemp) return
      for (const key in e) {
        this.dataTemp.e[key] = e[key]
      }
      this.hideType(e.type)
      // this.$emit('setConfig', this.dataTemp)
      this.$refs.from.setConfig(this.dataTemp)
      console.log('setting - setValuesetValuesetValuesetValuesetValue')
    },
    setData(e) {
      this.dataTemp = e
      // const data = this.data.arr
      const data = []
      this.data.arr.forEach((el, index) => {
        el.visibility = e.e[el.name] != undefined
        el.value = e.e[el.name]
        if (el.name == 'type') {
          if (e.e.typeArr) {
            el.visibility = true
            el.option = []
            e.e.typeArr.forEach(es => el.option.push({
              value: es,
              label: es
            }))
          } else {
            el.visibility = false
          }
        }
        if (e.e.category == 'checkbox' && el.name == 'max') {
          el.value = e.e.option.length
        }
        console.log(el, el.name, el.visibility, e.e[el.name])
        // this.$set(this.data.arr, index, el)
        // return JSON.parse(JSON.stringify(el))
        data.push(el)
        // return el
      })
      this.data.arr = data
      this.optionArr = e.e.option
      this.EleIndex = e.i
      // this.$refs.from.setFormValue(this.data.arr)
    },
    hideType(name) {
      /*  if (name && this.dataTemp.e.typeArr) {
        console.log(this.data.arr)
        this.data.arr.map((es, index) => {
          es.visibility = true
          this.dataTemp.e.hideType[name].forEach(et => {
            if (es.name == et) {
              es.visibility = false
            }
          })
          console.log(es, es.name, es.visibility)
          // return JSON.parse(JSON.stringify(es))
          return es
        })
      } */
    }
  }
}
</script>
<style>
  .config-content-input{
     margin-bottom: 0;
  }
</style>
<style lang='scss' scoped>
  .dictionary{
    .title{
      line-height: 36px;
      padding: 0 0 10px;
      color: #606266;
      font-size: 14px;
      font-weight: 700;
    }
    .line{
      line-height: 36px;
      text-align: center;
    }
    i{
      color: #f56c6c;
      font-size: 20px;
      line-height: 36px;
      margin-left: 10px;
      cursor: pointer;
    }
    .but-box{
      text-align: center;
      display: block;
      .but{
        margin-top: 11px;
        margin-left: -30px;
      }
    }
    .list{
      margin-bottom: 5px;
      display: inline-block;
    }
  }

.config-content{
    width: 90%;
    margin: 0 auto;
    /* height: 100%;
    background: #ffffff;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 2;
    padding: 20px;
    padding-top: 5px;
    transition: left .3s;
    -moz-transition: left .3s;
    -webkit-transition: left .3s;
    -o-transition: left .3s; */
    .config-back{
        cursor:Pointer;
        width: 14px;
        height: 100%;
        color:#188ae2;
        position: absolute;
        right: 0;
        top: 0;
        font-size: 14px;
        &::after{
          content:'';
          display: inline-block;
          height: 100%;
          width: 100%;
          background: #f2f2f2;
          position: absolute;
          right: 0;
          top: 0;
        }
        &::before{
          position: absolute;
          top: 50%;
          right: 0;
          z-index: 2;
        }
    }
    &.config-close{
      left:-100%;
    }
  }
  </style>

